﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.SessionState;
using Core;
using KhoaLuan.Utils;
using DataAccess;
namespace KhoaLuan
{
    public static class MethodExt
    {
        public static void SetCurrentUser(this HttpSessionState session, tbUsers user)
        {
            session["currentUser"] = user;
        }

        private static int[] GetOrdinals(List<PropertyInfo> objProperties, IDataReader dr)
        {
            int[] arrOrdinals = new int[objProperties.Count];
            if (dr != null)
            {
                int count = objProperties.Count - 1;
                for (int i = 0; i <= count; i++)
                {
                    arrOrdinals[i] = -1;
                    try
                    {
                        arrOrdinals[i] = dr.GetOrdinal(objProperties[i].Name);
                    }
                    catch   // property does not exist in datareader
                    {

                    }
                }
            }
            return arrOrdinals;
        }

        public static T ToObject<T>(this IDataReader dr) where T : class, new()
        {
            T DoiTuong = new T();
            var objProperties = typeof(T).GetProperties();
            T Item = new T();
            if (dr != null && dr.Read())
            {
                foreach (var pro in objProperties)
                {
                    var pos = dr.GetOrdinal(pro.Name);
                    var temp = dr.IsDBNull(pos) ? null : dr[pro.Name];
                    pro.SetValue(Item, temp, null);
                }
                return Item;
            }
            if (dr != null) dr.Close();
            return null;
        }
        public static List<T> ToList<T>(this IDataReader dr) where T : class, new()
        {
            return ToList<T, List<T>>(dr);
        }

        public static C ToList<T, C>(this IDataReader dr)
            where T : class, new()
            where C : ICollection<T>, new()
        {
            C listObject = new C();
            if (dr == null)
                return listObject;
            T Item;
            var listThuocTinh = typeof(T).GetProperties();

            while (dr.Read())
            {
                Item = new T();
                foreach (var pro in listThuocTinh)
                {
                    var pos = dr.GetOrdinal(pro.Name);
                    var temp = dr.IsDBNull(pos) ? null : dr[pro.Name];
                    pro.SetValue(Item, temp, null);

                }
                listObject.Add(Item);
            }
            if (dr != null)
            {
                dr.Close();
            }
            return listObject;
        }


        public static tbUsers GetCurrentUser(this HttpSessionState session)
        {
            return UserLogin.GetCurrentUser;
        }
        public static void Function(this HttpSessionState session, tbUsers user)
        {
            session["function"] = user;
        }
        public static tbUsers Function(this HttpSessionState session)
        {
            return session["function"] as tbUsers;
        }
    }
}